<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleAddWindow(0)">
        添加<i class="el-icon-plus el-icon--right" />
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="60">
        <template slot-scope="{row}">
          <span>{{ row.Id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="用户昵称">
        <template slot-scope="{row}">
          <span>{{ row.Username }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="登陆邮箱">
        <template slot-scope="{row}">
          <span>{{ row.Email }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" label="注册日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.RegisterTime }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" label="注册IP" align="center">
        <template slot-scope="{row}">
          <span>{{ row.RegisterIp }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" label="上次登陆IP" align="center">
        <template slot-scope="{row}">
          <span>{{ row.LastLoginIp }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" label="上次登陆时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.LastLoginTime }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="120">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.Status }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="handlePasswordWindow(row)"
          >
            修改密码
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleAddWindow(row.Id)"
          >
            禁用
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleDel(row.Id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗  -->
    <el-dialog title="新增管理员" :visible.sync="dialogAddFormVisible">
      <el-form ref="addForm" :rules="rules" :model="addData" label-position="left" label-width="90px" style="width: 800px; margin-left:50px;">
        <el-form-item label="用户角色" prop="role_id">
          <el-select v-model="addData.role_id" placeholder="请选择">
            <el-option
              v-for="item in roleOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addData.username" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="登陆邮箱" prop="email">
          <el-input v-model="addData.email" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="登陆密码" prop="password">
          <el-input type="password" v-model="addData.password" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="资金密码" prop="fund_password">
          <el-input type="password" v-model="addData.fund_password" style="width: 380px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="handleAddSubmit()">
          Confirm
        </el-button>
      </div>
    </el-dialog>

    <!-- 弹窗  -->
    <el-dialog title="修改密码" :visible.sync="dialogPasswordFormVisible">
      <el-form ref="passwordForm" :rules="rules" :model="addData" label-position="left" label-width="90px" style="width: 800px; margin-left:50px;">
        <el-form-item label="当前用户" prop="currentUsername">
          <span style="color: red;">{{currentPasswordUser.Email}}</span>
        </el-form-item>
        <el-form-item label="登陆密码" prop="password">
          <el-input type="password" v-model="addData.password" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="资金密码" prop="fund_password">
          <el-input type="password" v-model="addData.fund_password" style="width: 380px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="handlePasswordSubmit()">
          Confirm
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {adminUserList, adminUserAdd, adminUserPassword, adminUserDel} from '@/api/admin'
import waves from '@/directive/waves'
import {developMenuDel} from "@/api/develop";

export default {
  name: 'AdminLogList',
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '0': '禁用',
        '1': '启用'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      currentAction: 'add',
      listQuery: {
        page_index: 1,
        page_size: 10
      },
      currentPasswordUser: {},
      passwordData: {
        id: 0,
        password: 0,
        fund_password: '',
        current_fund_password: ''
      },
      addData: {
        id: 0,
        role_id: 0,
        username: '',
        email: '',
        password: '',
        fund_password: ''
      },
      roleOption: [],
      dialogAddFormVisible: false,
      dialogPasswordFormVisible: false,
      rules: {
        role_id: [{ required: true, message: '需要选择角色', trigger: 'blur' }],
        username: [{ required: true, message: '需要填写用户名', trigger: 'blur' }],
        email: [{ required: true, message: '需要填写邮箱', trigger: 'blur' }],
        password: [{ required: true, message: '需要填写登陆密码', trigger: 'blur' }],
        fund_password: [{ required: true, message: '需要填写资金密码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetAddData() {
      this.addData = {
        role_id: 0,
        username: '',
        email: '',
        password: '',
        fund_password: ''
      }
    },
    async getList() {
      this.listLoading = true
      const { data } = await adminUserList(this.listQuery)
      this.list = data.data
      this.roleOption = data.role_option
      this.listLoading = false
    },
    handleAddWindow() {
      this.resetAddData()
      this.dialogAddFormVisible = true
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate()
      })
    },
    handleAddSubmit() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          adminUserAdd(this.addData).then((response) => {
            if (response.success) {
              this.dialogAddFormVisible = false
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
              this.getList()
            } else {
              this.$message({
                type: 'error',
                message: response.msg
              })
            }
          }).catch(err => {
            this.$message({
              type: 'error',
              message: err
            })
          })
        }
      })
    },
    handlePasswordWindow(item) {
      this.resetPasswordData()
      this.passwordData.id = item.Id
      this.currentPasswordUser = item
      this.dialogPasswordFormVisible = true
      this.$nextTick(() => {
        this.$refs['passwordForm'].clearValidate()
      })
    },
    handlePasswordSubmit() {
      this.$refs['passwordForm'].validate((valid) => {
        if (valid) {
          adminUserPassword(this.addData).then((response) => {
            if (response.success) {
              this.dialogAddFormVisible = false
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
              this.getList()
            } else {
              this.$message({
                type: 'error',
                message: response.msg
              })
            }
          }).catch(err => {
            this.$message({
              type: 'error',
              message: err
            })
          })
        }
      })
    },
    resetPasswordData() {
      this.currentPasswordUser = {}
      this.passwordData = {
        id: 0,
        password: 0,
        fund_password: '',
        current_fund_password: ''
      }
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除选择用户, 并删除角色/权限分配, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        adminUserDel(id).then((response) => {
          if (response.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: response.msg
            })
          }
        }).catch(err => {
          this.$message({
            type: 'success',
            message: '对不起,未知错误'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
w.
<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
